package com.group.service;

import com.group.controller.BaseController;
import com.group.dao.LeaderBoardDao;
import com.group.model.Out.LeaderBoardOut;
import com.group.model.SystemPara;
import com.group.model.User;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LeaderBoardService {

    @Autowired
    LeaderBoardDao leaderBoardDao;
    @Autowired
    UserService userService;


    /**
     * 分页获取收益排行榜
     * @return
     */
    public List<LeaderBoardOut> getUBLB(){
        List<LeaderBoardOut> bbllist = leaderBoardDao.queryUBLB();
        return bbllist;
    }

    /**
     * 分页获取财富排行榜
     * @return
     */
    public List<LeaderBoardOut> getWealth(){
        List<LeaderBoardOut> bbllist = leaderBoardDao.queryWealth();
        return bbllist;
    }

    /**
     * 返佣排行中，我的排名
     * @param userId
     * @return
     */
    public Map<String,Object> getMyRebate(Long userId){
        List<LeaderBoardOut> bbllist = leaderBoardDao.queryRebate();
        Map<String,Object> ret = new HashMap<>();
        for (LeaderBoardOut s : bbllist) {
            if(s.getUserId().equals(userId)){
                int a = bbllist.indexOf(s);
                ret.put("我的排名",a+1);
                ret.put("手机号",s.getTel());
                ret.put("返佣",s.getNum());
                ret.put("头像",s.getHeadImage());
                break;
            }
        }
        if(ret.isEmpty()){
            User user = userService.getUser(userId);
            LeaderBoardOut s = leaderBoardDao.queryMyRebate(userId);
            ret.put("我的排名","未上榜!");
            ret.put("手机号",user.getTel());
            ret.put("头像",user.getHeadimage());
            if(s == null){
                ret.put("返佣",0);
                return ret;
            }
            ret.put("返佣",s.getNum());
        }
        return ret;
    }

    /**
     * 收益排行中，我的排名
     * @param userId
     * @return
     */
    public Map<String,Object> getMyUBLB(Long userId){
        List<LeaderBoardOut> bbllist = leaderBoardDao.queryUBLB();
        Map<String,Object> ret = new HashMap<>();
        for (LeaderBoardOut s : bbllist) {
            if(s.getUserId().equals(userId)){
                int a = bbllist.indexOf(s);
                ret.put("我的排名",a+1);
                ret.put("手机号",s.getTel());
                ret.put("收益",s.getNum());
                ret.put("头像",s.getHeadImage());
                break;
            }
        }
        if(ret.isEmpty()){
            LeaderBoardOut s = leaderBoardDao.queryMyUBLB(userId);
            User user = userService.getUser(userId);
            ret.put("我的排名","未上榜!");
            ret.put("手机号",user.getTel());
            ret.put("头像",user.getHeadimage());
            if(s == null){
                ret.put("收益",0);
                return ret;
            }
            ret.put("收益",s.getNum());
        }
        return ret;
    }

    /**
     * 财富排行中，我的排名
     * @param userId
     * @return
     */
    public Map<String,Object> getMyWealth(Long userId){
        List<LeaderBoardOut> bbllist = leaderBoardDao.queryWealth();
        Map<String,Object> ret = new HashMap<>();
        for (LeaderBoardOut s : bbllist) {
            if(s.getUserId().equals(userId)){
                int a = bbllist.indexOf(s);
                ret.put("我的排名",a+1);
                ret.put("手机号",s.getTel());
                ret.put("财富",s.getNum());
                ret.put("头像",s.getHeadImage());
                break;
            }
        }
        if(ret.isEmpty()){
            LeaderBoardOut s = leaderBoardDao.queryMyWealth(userId);
            ret.put("我的排名","未上榜!");
            ret.put("手机号",s.getTel());
            ret.put("财富",s.getNum());
            ret.put("头像",s.getHeadImage());
        }
        return ret;
    }

    public Map<String,Object> getMyWinning(Long userId){
        List<LeaderBoardOut> bbllist = leaderBoardDao.queryWinning();
        Map<String,Object> ret = new HashMap<>();
        for (LeaderBoardOut s : bbllist) {
            if(s.getUserId().equals(userId)){
                int a = bbllist.indexOf(s);
                ret.put("我的排名",a+1);
                ret.put("手机号",s.getTel());
                ret.put("抽奖收益",s.getNum());
                ret.put("头像",s.getHeadImage());
                break;
            }
        }
        if(ret.isEmpty()){
            LeaderBoardOut s = leaderBoardDao.queryMyWinning(userId);
            User user = userService.getUser(userId);
            ret.put("我的排名","未上榜!");
            ret.put("手机号",user.getTel());
            ret.put("头像",user.getHeadimage());
            if(s == null){
                ret.put("抽奖收益",0);
                return ret;
            }
            ret.put("抽奖收益",s.getNum());
        }
        return ret;
    }
}
